import requests
import time
from threading import Thread
from multiprocessing import Process

result = []

def request_baidu(index):
    url = "https://www.baidu.com/"
    body = ""
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36"
    }
    response = requests.get(url=url, headers=headers)
    print(f"当前请求序号：{index}, 返回结果状态码: {response.status_code}")
    result.append(response)


# 1.利用python实现一个多线程程序
if __name__== "__main__":  # 用来判断是不是在当前文件中执行。
    #start_time = time.time()
    thread_array = []
    for i in range(10):
        t = Thread(target=request_baidu, args=(i, ))
        thread_array.append(t)
        t.start()
    for t in thread_array:
        t.join
    print("done!")
    print(result)
    #end_time = time.time()
    #print(f"execute thread time: {end_time - start_time}s.")

# 2.将多线程程序改为多进程程序
    #start_time = time.time()
    #for i in range(10):
        #p = Process(target=request_baidu, args=(i, ))
        #p.start()
    #end_time = time.time()
    #print(f"execute process time: {end_time - start_time}s.")

